package com.magic.java8.stream;

import org.junit.Test;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author mzk
 * @Description
 * @create 2021-04-01 13:18
 */
public class StreamTest {

    @Test
    public void test1() {
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        System.out.println("strings = " + strings);
        List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        System.out.println("filtered = " + filtered);
    }

    /**
     * 获取随机数
     */
    @Test
    public void test2() {
        Random random = new Random();
//        random.ints().limit(10).forEach(System.out::println);
//        random.ints(0, 10).distinct().limit(10).sorted().forEach(System.out::println);

        List<Integer> list = Arrays.asList(1, 3, 4, 5, 7, 8, 10, 12);

        int[] ints = random.ints(0, list.size() - 1).distinct().limit(5).toArray();
        System.out.println("Arrays.toString(ints) = " + Arrays.toString(ints));
    }

    @Test
    public void test3() {
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        // 获取空字符串的数量
        long count = strings.parallelStream().filter(string -> !string.isEmpty()).count();
        System.out.println("count = " + count);
    }

    @Test
    public void test4() {
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);

        IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();

        System.out.println("列表中最大的数 : " + stats.getMax());
        System.out.println("列表中最小的数 : " + stats.getMin());
        System.out.println("所有数之和 : " + stats.getSum());
        System.out.println("平均数 : " + stats.getAverage());
    }

    @Test
    public void testDate() {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDateTime.getDayOfMonth() = " + localDateTime.getDayOfMonth());
    }

    @Test
    public void testOther() {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            list.add(i);
        }
        list.stream().forEach(e -> {
            if (e == 1) {
                return;
            }
            System.err.println(e);
        });
    }
}
